package com.benjaminwan.read.free.model.bean

import android.text.Html
import android.text.TextUtils
import com.benjaminwan.read.free.utils.HiUtils.fromHtmlAndStrip

data class ContentQuote(
    val postText: String,
    val authorAndTime: String,
    val tid: String,
    val postId: String
) : ContentAbs() {
    var author: String = ""
    var to: String = ""
    var time: String = ""
    var text: String
    override val content: String = "${postText}${authorAndTime}"
    override val copyText: String
        get() = "『" + fromHtmlAndStrip(content).toString() + "』"

    val isReplyQuote: Boolean
        get() = !TextUtils.isEmpty(author) && !TextUtils.isEmpty(text)

    init {
        //replace chinese space and trim
        text = Html.fromHtml(postText, Html.FROM_HTML_MODE_LEGACY).toString().replace("　", " ")
            .replace(160.toChar().toString(), " ").trim()
        if (!TextUtils.isEmpty(authorAndTime) && authorAndTime.contains("发表于")) {
            author = authorAndTime.substring(0, authorAndTime.indexOf("发表于")).trim()
            time = authorAndTime.substring(authorAndTime.indexOf("发表于") + "发表于".length)
                .trim()
        }
        if (text.startsWith("回复")) {
            text = text.substring("回复".length).trim()
            //this is not accurate, will use postId if available
            val idx = text.indexOf("    ")
            if (idx in 1..9) {
                to = text.substring(0, idx).trim { it <= ' ' }
            } else if (text.indexOf(" ") > 0) {
                to = text.substring(0, text.indexOf(" ")).trim()
            }
            if (!TextUtils.isEmpty(to)) text = text.substring(to!!.length + 1).trim()
        }
    }
}